package com.xingfudazi.web.controller.app;

import com.xingfudazi.app.service.IAppMessageService;
import com.xingfudazi.app.service.IAppNoticeService;
import com.xingfudazi.common.core.domain.Result;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;

/**
 * 轮询
 *
 * @author YuJiaMing
 * date 2024年06月23日 00:36
 */
@RestController
@RequestMapping("/app/polling")
public class PollingController implements InitializingBean {

    @Autowired
    private IAppMessageService appMessageService;

    @Autowired
    private IAppNoticeService appNoticeService;

    private Map<PollingTypeEnum, Function<Object, Object>> pollingFunction;

    @Override
    public void afterPropertiesSet() throws Exception {
        pollingFunction = new HashMap<>();
        pollingFunction.put(PollingTypeEnum.NOTICE, (item) -> appNoticeService.polling());
        pollingFunction.put(PollingTypeEnum.MESSAGE, (item) -> appMessageService.getUnReadNum());

    }


    enum PollingTypeEnum {

        /**
         * 通知
         */
        NOTICE,

        /**
         * 消息站内信
         */
        MESSAGE
    }

    /**
     * 轮询
     */
    @GetMapping()
    public Result<?> polling(PollingTypeEnum type) {
        Object result = pollingFunction.get(type).apply(null);
        return Result.success(result);
    }

}
